# Copy a list of files from one directory to another. Relative file paths are maintained.
macro(COPY_FILES target file_list source_dir target_dir)
  foreach(FILENAME ${file_list})
    set(source_file ${source_dir}/${FILENAME})

    # Remove the target file path component.
    get_filename_component(target_name ${FILENAME} NAME)
    set(target_file ${target_dir}/${target_name})

    COPY_SINGLE_FILE(${target} ${source_file} ${target_file})
  endforeach()
endmacro()

macro(COPY_SINGLE_FILE target source_file target_file)
  string(FIND ${source_file} "$<CONFIGURATION>" _pos)
  if(NOT ${_pos} EQUAL -1)
    # Must test with an actual configuration directory.
    string(REPLACE "$<CONFIGURATION>" "Release" existing_source_file ${source_file})
    if(NOT EXISTS ${existing_source_file})
      string(REPLACE "$<CONFIGURATION>" "Debug" existing_source_file ${source_file})
    endif()
  else()
    set(existing_source_file ${source_file})
  endif()

  if(IS_DIRECTORY ${existing_source_file})
    add_custom_command(
      TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_directory "${source_file}" "${target_file}"
      VERBATIM
      )
  else()
    add_custom_command(
      TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different "${source_file}" "${target_file}"
      VERBATIM
      )
  endif()
endmacro()

macro(GenHookerFiles DetourFunc TargetAddress)
    file(READ ${HOOKENTRY_TEMPLATE} FILE_CONTENT)
    string(REPLACE "XXX" "${DetourFunc}" MODIFIED_CONTENT "${FILE_CONTENT}")
    string(REPLACE "0x12345678" ${TargetAddress} MODIFIED_CONTENT "${MODIFIED_CONTENT}")
    file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/HookEntry.hpp" "${MODIFIED_CONTENT}")

    file(READ ${INJECTHOOKER_TEMPLATE} FILE_CONTENT)
    string(REPLACE "Eng_Hooker/MinHook.h" "${CMAKE_CURRENT_SOURCE_DIR}/../Eng_Hooker/MinHook.h" MODIFIED_CONTENT "${FILE_CONTENT}")
    string(REPLACE "0x12345678" ${TargetAddress} MODIFIED_CONTENT "${MODIFIED_CONTENT}")
    file(WRITE "${CMAKE_CURRENT_SOURCE_DIR}/InjectHooker.cpp" "${MODIFIED_CONTENT}")
endmacro()

macro(DelayAndConfig)
    set(OUTPUT_BIN_FILES
        "${PROJECT_NAME}.dll"
        "${PROJECT_NAME}.pdb"
    )
    list(APPEND OUTPUT_BIN_FILES ${DLL_FILENAME})

    # 链接第三方库
    target_link_libraries(${PROJECT_NAME} ${Hooker_LIBRARIES})
    COPY_FILES("${PROJECT_NAME}" "${OUTPUT_BIN_FILES}" "${TARGET_OUT_DIR}" "${CMAKE_SOURCE_DIR}/Bin")
endmacro()